import java.util.Arrays;

/**
 * Created with IntelliJ IDEA
 * Description:Exercise
 * User:22954
 * Date:2024-01-05
 * Time:15:49
 */
public class Test {
    public static void main(String[] args) {
//        exercise1();//方法transform，数组为参数，循环将每个元素乘2
//        exercise2();//方法avg，以数组为参数，返回所有元素平均值
//        exercise3();//调整数组顺序使奇数在偶数前，不考虑大小顺序
//        exercise4();//实现二分查找
//        exercise5();//给定数组nums和一个整形目标target，在数组中找出和为目标值target的两个整数，返回下标
//        exercise6();//给定一个非空整数数组，除某元素外，其他元素均出现两次，找出该元素
//        exercise7();//给定一个大小为n的数组，找到其多数的元素
        exercise8();//给定一个数组，判断数组中是否存在连续三个元素都是奇数，返回判断
    }

    public static void exercise1() {
        int[] array = {1, 2, 3, 4, 5, 6, 7};
        transform(array);
        System.out.println(Arrays.toString(array));
    }

    public static void transform(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] *= 2;
        }
    }

    public static void exercise2() {
        int[] array = {1, 2, 3, 4, 5, 6, 7};
        float average = avg(array);
        System.out.println(average);
    }

    public static float avg(int[] arr) {
        float sum = 0;
        for (int x : arr) {
            sum += x;
        }
        return sum / arr.length;
    }

    public static void exercise3() {
        int[] array = {1, 2, 3, 4, 5, 6, 7};
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            while (left < right && array[left] % 2 == 1) {
                left++;
            }
            while (left < right && array[right] % 2 == 0) {
                right--;
            }
            int tmp = array[right];
            array[right] = array[left];
            array[left] = tmp;
        }
        System.out.println(Arrays.toString(array));
    }

    public static void exercise4() {
        int[] array = {1, 2, 3, 4, 5, 6, 7};
        int index = binarySearch(array, 0);
        System.out.println(array[index]);
    }

    public static int binarySearch(int[] arr, int x) {
        int left = 0;
        int right = arr.length - 1;
        int mid = (left + right) >>> 1;
        while (left <= right) {
            if (arr[mid] == x) {
                return mid;
            } else if (arr[mid] < x) {
                left = mid + 1;
                mid = (left + right) >>> 1;
            } else {
                right = mid - 1;
                mid = (left + right) >>> 1;
            }
        }
        return -1;
    }

    public static void exercise5() {
        int[] nums = {1, 2, 3, 5, 7, 8, 14};
        int target = 11;
        int[] ret = findPos(nums, target);
        System.out.println(Arrays.toString(ret));
    }

    public static int[] findPos(int[] arr, int x) {
        int[] pos = {-1, -1};
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] + arr[j] == x) {
                    pos[0] = i;
                    pos[1] = j;
                    return pos;
                }
            }
        }
        return pos;
    }

    public static void exercise6() {
        int[] array = {1, 1, 4, 8, 4, 6, 6};
        int single = findSingle(array);
        System.out.println(single);
    }

    public static int findSingle(int[] arr) {
        int ret = 0;
        for (int x : arr) {
            ret ^= x;
        }
        return ret;
    }

    public static void exercise7() {
        int[] array = {10, 9, 9, 9, 10};
        int ret = majorityElement(array);
        System.out.println(ret);
    }

    public static int majorityElement(int[] arr) {
        int count = 1;
        int ret = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (count == 0) {
                ret = arr[i];
                count++;
            } else {
                if (ret == arr[i]) {
                    count++;
                } else {
                    count--;
                }
            }
        }
        return ret;
    }

    public static void exercise8() {
        int[] array = {1, 2, 3, 5, 9};
        boolean ret = threeConsecutiveOdds(array);
        System.out.println(ret);
    }

    public static boolean threeConsecutiveOdds(int[] arr) {
        int count = 0;
        for (int x : arr) {
            if (x % 2 == 1) {
                count++;
            } else {
                count = 0;
            }
            if (count == 3) {
                return true;
            }
        }
        return false;
    }
}
